package com.joysuch.wwyt.api.base.controller;

import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import com.joysuch.wwyt.mh.bean.MhMonitoringRealTimeListBean;
import com.joysuch.wwyt.mh.bean.MonitorPointHistoryQueryBean;
import com.joysuch.wwyt.mh.bean.MonitorPointQueryBean;
import com.joysuch.wwyt.mh.dto.MhMonitoringPointDto;
import com.joysuch.wwyt.mh.service.MhMonitoringPointService;
import com.joysuch.wwyt.mh.service.MhMonitoringRealTimeService;
import com.joysuch.wwyt.mh.storage.IMonitorPointDataService;
import com.joysuch.wwyt.mh.storage.MonitorPointData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author jignwk
 * @since 2022/03/08 14:48
 */
@RestController
@RequestMapping("/api/base/point")
@Api(tags = "流程引擎的数据源接口")
public class PointController {

    @Autowired
    private MhMonitoringRealTimeService mhMonitoringRealTimeService;
    @Autowired
    private MhMonitoringPointService mhMonitoringPointService;
    @Autowired
    private IMonitorPointDataService monitorPointDataService;


    @GetMapping("/list")
    @ApiOperation(value = "监测点列表")
    public ResultBean list(Integer page, Integer size, String name, String pointCode, Long monitoringTypeId, Long tenantId, String orgCode) {
        Page<MhMonitoringRealTimeListBean> list = mhMonitoringRealTimeService.list(QPageRequest.of(page, size), name, pointCode, monitoringTypeId, tenantId, orgCode);
        return ResultBean.pageData(list.getContent(), list.getTotalElements());
    }

    @GetMapping("/getById")
    @ApiOperation(value = "根据监测点id获取监测点详细信息")
    public ResultBean MhMonitoringPointBeanDto(Long id) {
        try {
            MhMonitoringPointDto mhMonitoringPointById = mhMonitoringPointService.getMhMonitoringPointById(id);
            return ResultBean.success(mhMonitoringPointById);
        } catch (ApiBusinessException e) {
            return ResultBean.fail(e.getCode(), e.getMessage());
        }
    }

    @PostMapping("/value-his")
    public ResultBean valueHis(@RequestBody MonitorPointHistoryQueryBean bean) {
        try {
            List<MonitorPointData> list = monitorPointDataService.historyVals(bean.getTenantId(),
                    bean.getOrgCode(), bean.getTag(), bean.getStartTime(), bean.getEndTime(), bean.getFlag());
            return ResultBean.success(list);
        } catch (Exception e) {
            String platform = bean.getFlag() != null && bean.getFlag() == 0 ? "一体化平台" : "数据中心";
            return ResultBean.fail(404, "连接" + platform + "出错");
        }
    }

    @PostMapping("/value-real")
    public ResultBean valueReal(@RequestBody MonitorPointQueryBean bean) {
        Optional<MonitorPointData> monitorPointData = monitorPointDataService.realTimeVal(bean.getTenantId(),
                bean.getOrgCode(), bean.getTag());
        return ResultBean.success(monitorPointData.orElse(null));
    }
}
